From 3a115b9f5e28ada7b0fed5d138c4f66800b18b1f Mon Sep 17 00:00:00 2001 From: robertlipe Date: Sun, 8 Jun 2014 20:07:39 +0000 Subject: [PATCH] Add format option to subrip. Enable regression test for that format. --- .../reference/track/gpx_subsecond-sample.gpx | 52 +------ .../track/gpx_subsecond-sample~subrip.srt | 18 +-- gpsbabel/subrip.cc | 139 +++++++++++------- gpsbabel/testo.d/subrip.test | 2 +- 4 files changed, 99 insertions(+), 112 deletions(-) diff --git a/gpsbabel/reference/track/gpx_subsecond-sample.gpx b/gpsbabel/reference/track/gpx_subsecond-sample.gpx index a0a4147f1..718ef24e7 100644 --- a/gpsbabel/reference/track/gpx_subsecond-sample.gpx +++ b/gpsbabel/reference/track/gpx_subsecond-sample.gpx @@ -11,7 +11,7 @@ 289.200000 - + 13.818100 trkpt-2011-07-02T17:47:25.000Z T @@ -19,7 +19,7 @@ 289.300000 - + 14.049800 trkpt-2011-07-02T17:47:25.000Z T @@ -27,7 +27,7 @@ 289.300000 - + 14.161100 trkpt-2011-07-02T17:47:25.000Z T @@ -35,7 +35,7 @@ 289.400000 - + 14.090400 trkpt-2011-07-02T17:47:25.000Z T @@ -43,54 +43,12 @@ 289.500000 - + 14.183800 trkpt-2011-07-02T17:47:25.000Z T 3d - diff --git a/gpsbabel/reference/track/gpx_subsecond-sample~subrip.srt b/gpsbabel/reference/track/gpx_subsecond-sample~subrip.srt index e44b07ed3..2d69cc6ff 100644 --- a/gpsbabel/reference/track/gpx_subsecond-sample~subrip.srt +++ b/gpsbabel/reference/track/gpx_subsecond-sample~subrip.srt @@ -1,24 +1,20 @@ 1 -00:00:00,000 --> 00:00:00,200 -0 km/h, 289 m +17:47:25,200 --> 17:47:25,400 --> 49.7 km/h 289 m 17:47:25 Lat=49.79469 Lon=9.83402 2 -00:00:00,200 --> 00:00:00,400 -289 m +17:47:25,400 --> 17:47:25,600 --> 50.6 km/h 289 m 17:47:25 Lat=49.79472 Lon=9.83400 3 -00:00:00,400 --> 00:00:00,600 -289 m +17:47:25,600 --> 17:47:25,800 --> 51.0 km/h 289 m 17:47:25 Lat=49.79474 Lon=9.83398 4 -00:00:00,600 --> 00:00:00,800 -289 m +17:47:25,800 --> 17:47:26,000 --> 50.7 km/h 289 m 17:47:25 Lat=49.79476 Lon=9.83396 5 -00:00:00,800 --> 00:00:01,000 -290 m -17:47:25 Lat=49.79479 Lon=9.83394 +17:47:26,000 --> 17:47:27,000 --> 51.1 km/h 289 m +17:47:26 Lat=49.79479 Lon=9.83394 + diff --git a/gpsbabel/subrip.cc b/gpsbabel/subrip.cc index d67d29e0e..dc51b1b31 100644 --- a/gpsbabel/subrip.cc +++ b/gpsbabel/subrip.cc @@ -2,6 +2,7 @@ Write points to SubRip subtitle file (for video geotagging) Copyright (C) 2010 Michael von Glasow, michael @t vonglasow d.t com + Copyright (C) 2014 Gleb Smirnoff, glebius @t FreeBSD d.t org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,6 +28,7 @@ static char* opt_videotime; static char* opt_gpstime; static char* opt_gpsdate; +static char* opt_format; static time_t time_offset; static int stnum; static gbfile* fout; @@ -51,36 +53,22 @@ sync_time(time_t arg_gpstime, char* arg_videotime) return result; } -static time_t -gps_to_video_time(time_t arg_gpstime) +static QDateTime +gps_to_video_time(QDateTime arg_gpstime) { - static time_t result; + QDateTime result = arg_gpstime; /* Converts a GPS timestamp to relative time in the video stream. */ - result = arg_gpstime - time_offset; + result.addSecs(time_offset); return result; } static void -subrip_write_duration(time_t starttime, time_t endtime) +subrip_write_duration(QTime starttime, QTime endtime) { /* Writes start and end time for subtitle display to file. */ - struct tm* tmptime; - - tmptime = gmtime(&starttime); - gbfprintf(fout, "%02d:%02d:%02d,000 --> ", tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec); - - tmptime = gmtime(&endtime); - gbfprintf(fout, "%02d:%02d:%02d,000\n", tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec); -} - -static void -subrip_write_time(time_t arg_time) -{ - /* Writes a timestamp to file. */ - struct tm* tmptime; + gbfprintf(fout, "%02d:%02d:%02d,%03d --> ", starttime.hour(), starttime.minute(), starttime.second(), starttime.msec()); - tmptime = gmtime(&arg_time); - gbfprintf(fout, "%02d:%02d:%02d", tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec); + gbfprintf(fout, "%02d:%02d:%02d,%03d --> ", endtime.hour(), endtime.minute(), endtime.second(), endtime.msec()); } static void @@ -89,43 +77,87 @@ subrip_prevwp_pr(const Waypoint* waypointp) /* Now that we have the next waypoint, we can write out the subtitle for * the previous one. */ - time_t starttime; - time_t endtime; + QDateTime starttime; + QDateTime endtime; + char *c; - if (prevwpp->GetCreationTime().toTime_t() >= time_offset) - /* if this condition is not true, the waypoint is before the beginning of - * the video and will be ignored - */ - { + /* If this condition is not true, the waypoint is before the beginning of + * the video and will be ignored + */ + if (prevwpp->GetCreationTime().toTime_t() < time_offset) + return; + starttime = gps_to_video_time(prevwpp->GetCreationTime()); + if (!waypointp) { + endtime = starttime.addSecs(1); + } else { + endtime = gps_to_video_time(waypointp->GetCreationTime()); + } + gbfprintf(fout, "%d\n", stnum); + stnum++; + subrip_write_duration(starttime.time(), endtime.time()); + + for (c = opt_format; *c != '\0' ; c++) { + char fmt; + + switch (*c) { + case '%': + fmt = *++c; + is_fatal(fmt == '\0', "No character after %% in subrip format"); + + switch (fmt) { + case 's': + if WAYPT_HAS(prevwpp, speed) + gbfprintf(fout, "%2.1f", MPS_TO_KPH(prevwpp->speed)); + else + gbfprintf(fout, "--.-"); + break; + case 'e': + if (prevwpp->altitude != unknown_alt) + gbfprintf(fout, "%4d", (int)prevwpp->altitude); + else + gbfprintf(fout, " -"); + break; + case 't': + { + QTime t = prevwpp->GetCreationTime().time(); + gbfprintf(fout, "%02d:%02d:%02d", t.hour(), t.minute(), t.second()); + break; + } + case 'l': + // The +.00005 is for rounding. + gbfprintf(fout, "Lat=%0.5lf Lon=%0.5lf", + prevwpp->latitude+.000005, prevwpp->longitude+.000005); + break; + case 'c': + if (prevwpp->cadence != 0) + gbfprintf(fout, "%3u", prevwpp->cadence); + else + gbfprintf(fout, " -"); + break; + case 'h': + if (prevwpp->heartrate != 0) + gbfprintf(fout, "%3u", prevwpp->heartrate); + else + gbfprintf(fout, " -"); + break; + } - starttime = gps_to_video_time(prevwpp->GetCreationTime().toTime_t()); - if (!waypointp) { - endtime = starttime + 1; - } else { - endtime = gps_to_video_time(waypointp->GetCreationTime().toTime_t()); - } - gbfprintf(fout, "%d\n", stnum); - stnum++; - subrip_write_duration(starttime, endtime); - if WAYPT_HAS(prevwpp, speed) { - gbfprintf(fout, "%d km/h", - (int)(MPS_TO_KPH(prevwpp->speed) + 0.5)); - } - if (prevwpp->altitude != unknown_alt) { - if WAYPT_HAS(prevwpp, speed) { - gbfprintf(fout, ", "); + break; + + case '\\': + fmt = *++c; + is_fatal(fmt == '\0', "No character after \\ in subrip format"); + switch (fmt) { + case 'n': + gbfprintf(fout, "\n"); + break; } - gbfprintf(fout, "%d m\n", - (int)(prevwpp->altitude + 0.5)); - } else if WAYPT_HAS(prevwpp, speed) { - gbfprintf(fout, "\n"); + break; + default: + gbfwrite(c, 1, 1, fout); } - subrip_write_time(prevwpp->GetCreationTime().toTime_t()); - gbfprintf(fout, " Lat=%0.5lf Lon=%0.5lf\n", - prevwpp->latitude + .000005, - prevwpp->longitude + .000005); - gbfprintf(fout, "\n"); } + gbfprintf(fout, "\n\n"); } /* callback functions */ @@ -230,6 +262,7 @@ arglist_t subrip_args[] = { {"video_time", &opt_videotime, "Video position for which exact GPS time is known (hhmmss, default is 0:00:00)", 0, ARGTYPE_STRING, ARG_NOMINMAX }, {"gps_time", &opt_gpstime, "GPS time at position video_time (hhmmss, default is first timestamp of track)", 0, ARGTYPE_STRING, ARG_NOMINMAX }, {"gps_date", &opt_gpsdate, "GPS date at position video_time (hhmmss, default is first timestamp of track)", 0, ARGTYPE_STRING, ARG_NOMINMAX }, + {"format", &opt_format, "Format for subtitles", "%s km/h %e m\n%t %l", ARGTYPE_STRING, ARG_NOMINMAX }, ARG_TERMINATOR }; diff --git a/gpsbabel/testo.d/subrip.test b/gpsbabel/testo.d/subrip.test index eb8dbcb1a..db19fc073 100644 --- a/gpsbabel/testo.d/subrip.test +++ b/gpsbabel/testo.d/subrip.test @@ -1,4 +1,4 @@ rm -f ${TMPDIR}/subrip.srt gpsbabel -i gpx -f ${REFERENCE}/track/gpx_subsecond-sample.gpx -o subrip -F ${TMPDIR}/subrip.srt # FIXME: This can't work right until we move to "real" subsecond support. -# compare ${REFERENCE}/track/gpx_subsecond-sample~subrip.srt ${TMPDIR}/subrip.srt +compare ${REFERENCE}/track/gpx_subsecond-sample~subrip.srt ${TMPDIR}/subrip.srt -- 2.30.2